﻿using System;
using UnityEngine;

public class EnemyBehavior : MonoBehaviour
{
    public GameObject Bullet = null;

    private float FireRate = 0.5f;
    private float MoveSpeed = 70f;
    private float Health = 10;

    private GameObject Player { get; set; }

    private float lastShot = 0.0f;
    private bool playerInSight;
    private int diff = 0;

    private void Start()
    {
        Player = GameObject.Find("Player");
        diff = (int)(Time.time / 10);
        //MoveSpeed += (int)(Time.time / 60);
        Health += (int)(Time.time / 30);
    }

    private float amplitudeX = 40;
    private float amplitudeY = 20;
    private float frequency = 0.25f;
    private LayerMask layerMask = 1 << 8;

    private void FixedUpdate()
    {
        if (!gameObject.renderer.isVisible)
        {
            rigidbody2D.velocity = Vector2.zero;
            return;
        }

        var item = Physics2D.Linecast(transform.position, Player.transform.position, layerMask);
        playerInSight = (item.distance <= 100 && item.collider.CompareTag("Player"));

        if (playerInSight)
            Debug.DrawLine(transform.position, Player.transform.position, Color.red);

        Move();

        Fire();

        if (Health <= 0)
            Destroy(gameObject);
    }

    public void Fire()
    {
        //if (!playerInSight) return;
        //if (Time.time > FireRate + lastShot)
        //{
        //	var bullet = Instantiate(Bullet, new Vector3(this.transform.position.x, this.transform.position.y, 0), Quaternion.identity) as GameObject;

        //	float angleOrig = MathHelper.GetAngle(this.transform.position, Player.transform.position);
        //	bullet.rigidbody2D.AddForce(MathHelper.VectorFromAngle(angleOrig) * 6500);
        //	bullet.GetComponent<BulletBehavior>().Damage = 2 + diff;
        //	bullet.GetComponent<BulletBehavior>().SecondsAlive = 2;

        //	bullet.transform.rotation = transform.rotation;

        //	lastShot = Time.time;
        //}
    }

    private void Move()
    {
        rigidbody2D.velocity = Vector2.zero;
        if (playerInSight)
        {
            rigidbody2D.velocity = (Player.transform.position - transform.position).normalized * MoveSpeed * Time.deltaTime;

            var dir = rigidbody2D.velocity;
            var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
            var q = Quaternion.AngleAxis(angle, Vector3.forward);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, q, MoveSpeed * Time.deltaTime);
        }
        else
        {
            var x = amplitudeX * (Mathf.Cos(2 * Mathf.PI * frequency * Time.time) - Mathf.Cos(2 * Mathf.PI * frequency * (Time.time - Time.deltaTime))) * transform.right;
            var y = amplitudeY * (Mathf.Sin(2 * Mathf.PI * frequency * Time.time) - Mathf.Sin(2 * Mathf.PI * frequency * (Time.time - Time.deltaTime))) * transform.up;
            var v = (x + y) * (MoveSpeed * Time.deltaTime);
            rigidbody2D.velocity = v;
        }
    }

    private void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.CompareTag("playerProjectile"))
        {
            var bullet = col.GetComponent<ProjectileBehavior>();
            //if (bullet.Shooter != null)
            //	PlayerBehavior.Score += bullet.Score;

            Health -= bullet.Damage;

            Destroy(col.gameObject);
        }
    }
}